package com.iaiai.cobra.system.controller;

import com.iaiai.cobra.common.constant.YesOrNo;
import com.iaiai.cobra.common.vo.ResultVo;
import com.iaiai.cobra.core.BaseController;
import com.iaiai.cobra.repository.bean.Dept;
import com.iaiai.cobra.repository.bean.Menu;
import com.iaiai.cobra.repository.bean.po.MenuPo;
import com.iaiai.cobra.repository.constants.MenuTypeEnum;
import com.iaiai.cobra.web.params.PageParams;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created with MenuController.
 * Package: com.iaiai.cobra.system.controller
 * Author: iaiai
 * Create Time: 2024/7/4 21:53
 * QQ: 176291935
 * Email: 176291935@qq.com
 * Description:
 */
@RestController
@RequestMapping("/menu")
public class MenuController extends BaseController {

    @PostMapping("/list.json")
    public ResultVo list(@RequestBody(required = false) Menu params) {
        Map<String,Object> map = new HashMap<>();
        List<Menu> menus = serviceManager.menuService.queryMenus(params==null?null:params.getId());
        map.put("menus",menus);

        return success(map);
    }

    //查询菜单项，包含子，只查菜单，不包含功能项和子页面
    @PostMapping("/menu.json")
    public ResultVo menu() {
        Map<String,Object> map = new HashMap<>();
        List<MenuPo> menus = serviceManager.menuService.queryAllMenu();
        map.put("menus",menus);

        return success(map);
    }

    @PostMapping("/add.do")
    public ResultVo add(@RequestBody Menu menu) {
        if(menu==null
                || StringUtils.isEmpty(menu.getTitle())
                || StringUtils.isEmpty(menu.getCode())
                || menu.getType()==null
                || menu.getSeq()==null
                || menu.getCache()==null
                || menu.getShow()==null){
            return fail("参数异常:1");
        }

        if(menu.getType().intValue()== MenuTypeEnum.menu.getKey()
                || menu.getType().intValue()== MenuTypeEnum.childPage.getKey()){
            if(StringUtils.isEmpty(menu.getUrl()) || StringUtils.isEmpty(menu.getFilePath())){
                return fail("参数异常:2");
            }
        }

        menu.setCreateTime(new Date());
        menu.setModifyTime(menu.getCreateTime());
        menu.setDel(YesOrNo.NO.getValue());
        serviceManager.menuService.save(menu);

        return success();
    }

    @PostMapping("/detail.json")
    public ResultVo detail(@RequestBody Menu params) {
        if(params==null || params.getId()==null){
            return fail("参数异常");
        }

        return success(serviceManager.menuService.getById(params.getId()));
    }

    @PostMapping("/edit.do")
    public ResultVo edit(@RequestBody Menu menu) {
        if(menu==null
                || menu.getId()==null
                || StringUtils.isEmpty(menu.getTitle())
                || StringUtils.isEmpty(menu.getCode())
                || menu.getType()==null
                || menu.getSeq()==null
                || menu.getCache()==null
                || menu.getShow()==null){
            return fail("参数异常:1");
        }

        if(menu.getType().intValue()== MenuTypeEnum.menu.getKey()
                || menu.getType().intValue()== MenuTypeEnum.childPage.getKey()){
            if(StringUtils.isEmpty(menu.getUrl()) || StringUtils.isEmpty(menu.getFilePath())){
                return fail("参数异常:2");
            }
        }

        menu.setModifyTime(new Date());
        serviceManager.menuService.updateById(menu);

        return success();
    }

    @PostMapping("/del.do")
    public ResultVo del(@RequestBody Menu params) {
        if (params == null || params.getId() == null) {
            return fail("参数异常");
        }
        Menu menu = serviceManager.menuService.getById(params.getId());
        menu.setDel(YesOrNo.YES.getValue());
        serviceManager.menuService.updateById(menu);

        //删除角色下关联的数据
        serviceManager.roleMenuService.delMenu(menu.getId());

        return success();
    }

}
